package threaddemo;

public class People implements Runnable{

	private boolean flag;
	private Object bow;
	private Object chopsticks;
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public Object getBow() {
		return bow;
	}
	public void setBow(Object bow) {
		this.bow = bow;
	}
	public Object getChopsticks() {
		return chopsticks;
	}
	public void setChopsticks(Object chopsticks) {
		this.chopsticks = chopsticks;
	}
	
	public People() {
		super();
	}
	public People(boolean flag, Object bow, Object chopsticks) {
		super();
		this.flag = flag;
		this.bow = bow;
		this.chopsticks = chopsticks;
	}
	
	public void run(){
		if(flag){
			synchronized(bow){
				usebow(bow);
				synchronized(chopsticks){
					usechopsticks(chopsticks);
				}
			}
		}
		else{
			synchronized(chopsticks){
				usechopsticks(chopsticks);
				synchronized(bow){
					usebow(bow);
				}
			}
		}
		
	}
	
	private void usebow(Object bow){
		System.out.println(Thread.currentThread().getName()+" use bow");
	}
	private void usechopsticks(Object chostics){
		System.out.println(Thread.currentThread().getName()+" use chopstics");
	}
}
